using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class PlayerGUI : MonoBehaviour {

	public bool 		showTestUI 					= false;
	
	public bool 		showCrosshair 				= true;
	public Vector2 		CrosshairScale 				= new Vector2(0.15f,0.15f);
	public Texture2D 	CrosshairTexture;
	private Vector2 	CrosshairSize;
		/*public GUIStyle Healthbar;
	//public bool 		displayHealthbar 			= false;
	//Health Bar Variables
	public Texture2D 	HealthBarTexture;
	public Texture2D 	HealthBarBorderTexture;
	public Vector2 		HealthBarPosition 			= new Vector2(0,0);
	public Vector2 		HealthBarScale 				= new Vector2(1,1);
	private float		maxHealth 					= 300;
	// Health Bar Border Thickness
	// SKALL VARA PUBLIC!
	//!!! Tjockleken på kanterna i Y-led stämmer inte!
	public float 		HealthBarBorderSizeLeft 	= 100;
	public float 		HealthBarBorderSizeRight	= 100;
	public float 		HealthBarBorderSizeUp 		= 100;
	public float 		HealthBarBorderSizeDown 	= 50;
	
	private float 		Health;
	private Vector2 	HealthBarSize;
	*/
	//Duct Tape
	public Texture2D 	TapeBorderTexture;
	public Texture2D 	TapeRollTexture;
	public Texture2D 	TapeAddTexture;
	public Vector2 		TapeBorderPosition 			= new Vector2(0,200);
	public Vector2 		TapeRollPosition 			= new Vector2(0,0);
	public Vector2 		TapeBorderScale 			= new Vector2(1,1);
	public Vector2 		TapeRollScale 				= new Vector2(1,1);
	public Vector2		TapeRollSize;
	
	public Vector2		TapeAddSizeIncrease			= new Vector2(40,15);
	public Vector2		TapeAddMovement				= new Vector2(20,5);
	
	// Duct Tape Border Thickness
	// SKALL VARA PUBLIC!
	//!!! Tjockleken på kanterna i Y-led stämmer inte!
	public float 		TapeBorderSizeLeft 			= 100;
	public float 		TapeBorderSizeUp 			= 100;
	
	private Vector2 	TapeBorderSize;
	public int 			maxTapeSize = 10;
	private int 		TapeAmount = 3;
	private Vector2 	TapeSize;
	
	// Inventory
	public Texture2D	InventoryBorderTexture;
	public Vector2		InventoryPosition		= new Vector2(700,100);
	public Vector2		InventoryScale;
	public Vector2		InventorySpread;
	private Vector2		InventorySquareSize;
	private Texture2D	InventoryLeft;
	private Texture2D	InventoryRight;
	private Texture2D	InventoryUp;
	private Texture2D	InventoryDown;
	// Inventory Border Thickness
	// SKALL VARA PUBLIC!
	//!!! Tjockleken på kanterna i Y-led stämmer inte!
	public float 		InventoryBorderSizeLeft 	= 100;
	public float 		InventoryBorderSizeRight	= 100;
	public float 		InventoryBorderSizeUp 		= 100;
	public float 		InventoryBorderSizeDown 	= 50;
	
//	//Score
//	
//	public Texture2D[] hatTextures;
//	NetworkViewID myID;
//	
//	public struct Score
//	{
//		public NetworkViewID ID;
//		public int points;
//		public float position;
//		
//		public Score(NetworkViewID identification, int score, float Pos){
//			this.position = Pos;
//			this.ID = identification;
//			this.points = score;
//		}
//	}
//
//	private List<Score> PlayerScores = new List<Score>();
	
	void Start() {
		/*if (HealthBarTexture != null && HealthBarBorderTexture != null) {
			Health = maxHealth;
			HealthBarSize = new Vector2(HealthBarBorderTexture.width*HealthBarScale.x,HealthBarBorderTexture.height*HealthBarScale.y);
			HealthBarBorderSizeLeft 	*= HealthBarScale.x;
			HealthBarBorderSizeRight 	*= HealthBarScale.x;
			HealthBarBorderSizeUp 		*= HealthBarScale.y;
			HealthBarBorderSizeDown 	*= HealthBarScale.y;
		}
		 */
		if (TapeBorderTexture != null && TapeRollTexture != null && TapeAddTexture != null) {
			TapeBorderSize 	= new Vector2(TapeBorderTexture.width*TapeBorderScale.x,TapeBorderTexture.height*TapeBorderScale.y);
			TapeRollSize 	= new Vector2(TapeRollTexture.width*TapeRollScale.x,TapeRollTexture.height*TapeRollScale.y);
			TapeBorderSizeLeft *= TapeBorderScale.x;
			TapeBorderSizeUp   *= TapeBorderScale.y;
		}
		if (InventoryBorderTexture != null) {
			InventoryBorderSizeLeft 	*= InventoryScale.x;
			InventoryBorderSizeUp 		*= InventoryScale.y;
			InventoryBorderSizeRight 	*= InventoryScale.x;
			InventoryBorderSizeDown		*= InventoryScale.y;
			
			InventorySquareSize 		= new Vector2(InventoryBorderTexture.width*InventoryScale.x,InventoryBorderTexture.height*InventoryScale.y);
			InventorySpread 			= new Vector2(InventorySpread.x,InventorySpread.y);
		}
		
		if (CrosshairTexture != null) {
			CrosshairSize = new Vector2(CrosshairTexture.width*CrosshairScale.x,CrosshairTexture.height*CrosshairScale.y);
		}
		
//		ScoreListener.inst().add(delegate(ScoreEvent ev) {
//			setPlayer(ev.getPlayer(), ev.getScore(),null);
//		});
//		ScoreBoardListener.inst().add(ScoreBoardListener.Channel.ON_CONNECTED,delegate(ScoreBoardEvent ev){
//			addPlayer(ev.getPlayer());
//		});
//		ScoreBoardListener.inst().add(ScoreBoardListener.Channel.ON_DISCONNECTED,delegate(ScoreBoardEvent ev){
//			removePlayer(ev.getPlayer());
//		});
	}
	
	
	void Update(){
		if (Input.GetKeyDown(KeyCode.F1)){
			if (Screen.lockCursor == true) 
				Screen.lockCursor = false;
			else 
				Screen.lockCursor = true;
		}	
	}
	
	void OnGUI () {
		
		//CROSSHAIR-----------------------------------------------------------------------------------------------------------------
		if (showCrosshair == true && CrosshairTexture != null) {
			GUI.DrawTexture(new Rect(Screen.width/2-CrosshairSize.x/2,Screen.height/2-CrosshairSize.y/2,CrosshairSize.x,CrosshairSize.y),CrosshairTexture);
		}
		
		/*
		// HEALTHBAR----------------------------------------------------------------------------------------------------------------
		if (HealthBarTexture != null && HealthBarBorderTexture != null && displayHealthbar) {
			// Health Bar Area
			GUI.BeginGroup(new Rect(	HealthBarPosition.x,
										HealthBarPosition.y,
										HealthBarSize.x+100,
										HealthBarSize.y));
			//Health Bar
			GUI.DrawTexture(new Rect(	HealthBarBorderSizeLeft,
										HealthBarBorderSizeUp,
										(HealthBarSize.x-HealthBarBorderSizeLeft-HealthBarBorderSizeRight)*((Health/maxHealth)),
										HealthBarSize.y-HealthBarBorderSizeDown-HealthBarBorderSizeUp),
								HealthBarTexture,
								ScaleMode.ScaleAndCrop);
				
			// Health Bar Border
			GUI.DrawTexture(new Rect(	0,
										0,
										HealthBarSize.x,
										HealthBarSize.y),
								HealthBarBorderTexture,
								ScaleMode.StretchToFill);
				
			// Test Buttons
			if (showTestUI == true) {
				if (GUI.Button(new Rect(HealthBarSize.x+10,5,40,20),"+10")){
					if (Health < maxHealth)
					Health += 10; 	
				}
				if (GUI.Button(new Rect(HealthBarSize.x+10,30,40,20),"-10")){
					if (Health > 1) 
					Health -= 10; 	
				}
			}
			GUI.EndGroup();
		}
		*/
		// TAPE --------------------------------------------------------------------------------------------------------------------
		if (TapeBorderTexture != null && TapeRollTexture != null && TapeAddTexture != null) {
			// Duct Tape Area
			GUI.BeginGroup(new Rect(TapeBorderPosition.x,
								TapeBorderPosition.y,
								TapeBorderSize.x+100,
								TapeBorderSize.y));
		
			//Duct Tape Border
			GUI.DrawTexture(new Rect(	0,
										0,
										TapeBorderSize.x,
										TapeBorderSize.y),
								TapeBorderTexture,
								ScaleMode.StretchToFill);
		
			//Duct Tape
			GUI.DrawTexture(new Rect(	TapeBorderSizeLeft+TapeRollPosition.x,
										TapeBorderSizeUp+TapeRollPosition.y,
										TapeRollSize.x,
										TapeRollSize.y),
								TapeRollTexture,
								ScaleMode.StretchToFill);
		
			// Draw Tape Layers
		for (int i = 0; i < TapeAmount; i++) {
				GUI.DrawTexture(new Rect(	TapeBorderSizeLeft+TapeRollPosition.x-i*TapeAddMovement.x,
											TapeBorderSizeUp+TapeRollPosition.y-i*TapeAddMovement.y,
											TapeRollSize.x+i*TapeAddSizeIncrease.x,
											TapeRollSize.y+i*TapeAddSizeIncrease.y),
									TapeAddTexture,
									ScaleMode.StretchToFill);
		}
			
		// Test Buttons
		if (showTestUI == true) {
			if (GUI.Button(new Rect(TapeBorderSize.x+10,75,40,20),"+")){
				if (TapeAmount < maxTapeSize) 
					TapeAmount += 1; 	
			}
			if (GUI.Button(new Rect(TapeBorderSize.x+10,100,40,20),"-")){
				if (TapeAmount > 0) 
					TapeAmount -= 1;	
			}
			GUI.Label(new Rect(TapeBorderSize.x+10,125,40,20),TapeAmount.ToString(),"box");
		}
		GUI.EndGroup();
		}
		
		// INVENTORY-------------------------------------------------------------------------------------------
		if (InventoryBorderTexture != null) {
			GUI.BeginGroup(new Rect(InventoryPosition.x,
									InventoryPosition.y,
									InventorySquareSize.x*3+InventorySpread.x*2,
									InventorySquareSize.y*3+InventorySpread.y*2));
			//Inventory Border
			//UP
			GUI.DrawTexture(new Rect(	InventorySquareSize.x+InventorySpread.x,
										0,
										InventorySquareSize.x,
										InventorySquareSize.y),
								InventoryBorderTexture,
								ScaleMode.StretchToFill);
			if (InventoryUp != null) {
			GUI.DrawTexture(new Rect(	InventorySquareSize.x+InventorySpread.x+InventoryBorderSizeLeft,
										0+InventoryBorderSizeUp,
										InventorySquareSize.x-InventoryBorderSizeRight-InventoryBorderSizeLeft,
										InventorySquareSize.y-InventoryBorderSizeDown-InventoryBorderSizeUp),
								InventoryUp,
								ScaleMode.StretchToFill);
			}
			
			
			//DOWN
			GUI.DrawTexture(new Rect(	InventorySquareSize.x+InventorySpread.x,
										InventorySquareSize.y*2+InventorySpread.y*2,
										InventorySquareSize.x,
										InventorySquareSize.y),
								InventoryBorderTexture,
								ScaleMode.StretchToFill);
			if (InventoryDown != null) {
			GUI.DrawTexture(new Rect(	InventorySquareSize.x+InventorySpread.x+InventoryBorderSizeLeft,
										InventorySquareSize.y*2+InventorySpread.y*2+InventoryBorderSizeUp,
										InventorySquareSize.x-InventoryBorderSizeRight-InventoryBorderSizeLeft,
										InventorySquareSize.y-InventoryBorderSizeDown-InventoryBorderSizeUp),
								InventoryDown,
								ScaleMode.StretchToFill);
			}
			//LEFT
			GUI.DrawTexture(new Rect(	0,
										InventorySquareSize.y+InventorySpread.y,
										InventorySquareSize.x,
										InventorySquareSize.y),
								InventoryBorderTexture,
								ScaleMode.StretchToFill);
			if (InventoryLeft != null) {
			GUI.DrawTexture(new Rect(	0+InventoryBorderSizeLeft,
										InventorySquareSize.y+InventorySpread.y+InventoryBorderSizeUp,
										InventorySquareSize.x-InventoryBorderSizeRight-InventoryBorderSizeLeft,
										InventorySquareSize.y-InventoryBorderSizeDown-InventoryBorderSizeUp),
								InventoryLeft,
								ScaleMode.StretchToFill);
			}
			//RIGHT
			GUI.DrawTexture(new Rect(	InventorySquareSize.x*2+InventorySpread.x*2,
										InventorySquareSize.y+InventorySpread.y,
										InventorySquareSize.x,
										InventorySquareSize.y),
								InventoryBorderTexture,
								ScaleMode.StretchToFill);
			if (InventoryRight != null) {
			GUI.DrawTexture(new Rect(	InventorySquareSize.x*2+InventorySpread.x*2+InventoryBorderSizeLeft,
										InventorySquareSize.y+InventorySpread.y+InventoryBorderSizeUp,
										InventorySquareSize.x-InventoryBorderSizeRight-InventoryBorderSizeLeft,
										InventorySquareSize.y-InventoryBorderSizeDown-InventoryBorderSizeUp),
								InventoryRight,
								ScaleMode.StretchToFill);
			}
			
			GUI.EndGroup();
		}
		
		
		//SCORES-----------------------------------------------------------------------------------------------------
//		GUI.BeginGroup(new Rect(Screen.width-300,
//								50,
//								300,
//								Screen.height));
//		// Meter
//		GUI.Box(new Rect(		275,
//								100,
//								25,
//								Screen.height-200)," ");
//	
//		float end 	= Screen.height-125;		
//		float length = Screen.height-125-100;
//		
//		// Loop Through Players
//		for (int i = 0; i < PlayerScores.Count; i++) {
//			//PlayerScores.Sort((s1, s2) => s1.ID.CompareTo(s2.ID));
//			PlayerScores.Sort((s1, s2) => s1.points.CompareTo(s2.points));
//
//			Score SCORE = PlayerScores[i];
//			if (showTestUI == true){
//				SCORE.points = int.Parse(GUI.TextField(new Rect (25, 100+25*i, 25, 25), SCORE.points.ToString(),"box"));
//				if (GUI.Button(new Rect(0,100+25*i,25,25),"-")){
//				if (SCORE.points > 0)
//					SCORE.points--;
//				}
//				if (GUI.Button(new Rect(50,100+25*i,25,25),"+")){
//					SCORE.points++;
//				}	
//				GUI.Label(new Rect(75,100+25*i,75,25),"Player " + SCORE.ID,"box");
//				PlayerScores[i] = SCORE;
//			}
//			PlayerScores.Sort((s1,s2) => s1.points.CompareTo(s2.points));
//			
//			SCORE = PlayerScores[i];
//			Score SCORELEAST 	= PlayerScores[0]; 
//			Score SCOREMAX 		= PlayerScores[PlayerScores.Count-1];
//			if (SCORE.points > SCOREMAX.points-10)
//				SCORE.position = (length*0.3f)+((length*0.7f)/9)*(SCORE.points - (SCOREMAX.points-9));
//			else 
//				SCORE.position = ((length*0.3f)/((SCOREMAX.points-9)-SCORELEAST.points))*(SCORE.points - SCORELEAST.points);;
//			//GUI.Label(new Rect(175,end-SCORE.position,105,25),SCORE.ID.ToString(),"box");	
//			if(SCORE.ID==myID)
//				GUI.DrawTexture(new Rect(240,end-SCORE.position,40,40),hatTextures[0]);
//			else
//				GUI.DrawTexture(new Rect(240,end-SCORE.position,40,40),hatTextures[1]);
//		}
//		// Avgränsare
//		GUI.Label(new Rect(270,end-length*0.3f,30,25),"","box");
//		
//		// Add/Remove Players
//		if (showTestUI == true){
//			GUI.Label(new Rect(			75,
//										30,
//										75,
//										25),"PLAYERS","box");
//			
//			if (GUI.Button(new Rect(0,30,25,25),"-")){
//				if (PlayerScores.Count > 0){
//					PlayerScores.RemoveAt(PlayerScores.Count-1);
//				}
//			}
//			if (GUI.Button(new Rect(50,30,25,25),"+")){
//				PlayerScores.Add(new Score(new NetworkViewID(), 1, 0));
//			}
//			GUI.Label(new Rect(			25,
//										30,
//										25,
//										25),PlayerScores.Count.ToString(),"box");
//		}
//		
//		GUI.EndGroup();
		//------------------------------------------------
	}
	

	/*public void setHealth(float HP) {
		Health = HP;
	}
	
	public float getHealth(){
		return Health;	
	}*/
	
//	public void setMyID(NetworkViewID id){
//		myID = id;
//	}
//		
//	void addPlayer(NetworkViewID id){
//			PlayerScores.Add(new Score(id, 0,0));
//		}
//	
//	public void setPlayer(NetworkViewID id, int Points,NetworkEntityMgr nmgr){
//		bool newPlayer = true;
//		for (int i = 0; i < PlayerScores.Count; i++) {
//			Score SCORE = PlayerScores[i];		
//			if (SCORE.ID == id){
//				newPlayer = false;
//				SCORE.points = Points;
//				PlayerScores[i] = SCORE;
//				return;
//			}
//		}
//		if(newPlayer){
//			PlayerScores.Add(new Score(id, Points,0));			
//		}
//	}
//	
//	public void clearPlayers(){
//		PlayerScores.Clear();
//	}
//	
//	public bool removePlayer(NetworkViewID id){
//		for (int i = 0; i < PlayerScores.Count; i++) {
//			Debug.Log(PlayerScores[i].ID);
//			if(PlayerScores[i].ID == id){
//				PlayerScores.RemoveAt(i);
//				return true;
//			}
//		}
//		return false;
//	}
//	
//	public List<Score> getScoreBoard(){
//		return PlayerScores;
//	}	
//	
//	
//	public int getPlayerScore(NetworkViewID ID){
//			for (int i = 0; i < PlayerScores.Count; i++) {
//			Score SCORE = PlayerScores[i];		
//				if (SCORE.ID == ID){
//					//PlayerScores[i] = SCORE;
//					return SCORE.points;
//				}
//			}
//			return -1;
//	}
//	
//	public int getNumberOfPlayers(){
//		return PlayerScores.Count;	
//	}
	
	public void setInventory(int ID,Texture2D Image) {
		if (ID == 1) 	
			InventoryUp 	= Image;
		if (ID == 2) 	
			InventoryRight 	= Image;
		if (ID == 3) 	
			InventoryDown 	= Image;
		if (ID == 4) 	
			InventoryLeft 	= Image;
	}
		
		
	public void setTape(int Amount){
		TapeAmount = Amount;	
	}
	
	public int getTape(){
		return TapeAmount;	
	}
	
/*	public void setMaxHealth(float HP){
		maxHealth = HP;	
	}
	*/

}